package com.rocpenghua.excel.water;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 生成水印图片
 * @author lyt
 * 2017年7月19日
 */
public final class ImageUtils {
	
	
	
 public static void main(String[] args) throws IOException {
	 BufferedImage bufferedImage = createWaterMark(Arrays.asList("2022-07-18 14:26","山东","张学友"), new ImageParam());
	 ImageIO.write(bufferedImage,"png", new File("e:\\水印2.png"));
 }


	public static BufferedImage createWaterMark(List<String> contents, ImageParam imageParam) throws IOException{
		int imgWidth = imageParam.getImgWidth();
		int imgHeight = imageParam.getImgHeight();
		float alphaExcel = imageParam.getAlphaExcel();
		int colorR = imageParam.getColorR();
		int colorG = imageParam.getColorG();
		int colorB = imageParam.getColorB();
		Color colors = new Color(colorR, colorG, colorB);
		String fontName = imageParam.getFontName();
		int fontStyle = imageParam.getFontStyle();
		int size = imageParam.getSize();
		List<Integer> lefts = imageParam.getLefts();
		List<Integer> heights = imageParam.getHeights();
		int rotate = imageParam.getRotate();

		BufferedImage sourceImage = new BufferedImage(imgWidth, imgHeight, 1);

		Graphics2D g2 = sourceImage.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		sourceImage = g2.getDeviceConfiguration().createCompatibleImage(imgWidth, imgHeight, 3);
		g2 = sourceImage.createGraphics();
		AlphaComposite alphaChannel = AlphaComposite.getInstance(3, alphaExcel);
		g2.setComposite(alphaChannel);
		g2.setStroke(new BasicStroke(1.0F));
		g2.setColor(colors);
		g2.setFont(new Font(fontName, fontStyle, size));
		g2.rotate(Math.toRadians(rotate), imgWidth >> 1, imgHeight >> 1);
		for(int i = 0,s = contents.size();i<s;i++){
			g2.drawString(contents.get(i), lefts.get(i), sourceImage.getHeight() - heights.get(i) + (24*(i+1)));
		}
		g2.dispose();
		return sourceImage;
	}
}